<template>
  <div class="showItem-dormitory-details">
    <el-table
      :data="tableData"
      :header-cell-style="{ background: '#f5f7fa' }"
      border
      style="width: 100%"
    >
      <el-table-column prop="name" align="center" label="房间名称" />
      <el-table-column prop="rent" align="center" label="租金（元）" />
      <el-table-column prop="number" align="center" label="已住/可住">
        <template slot-scope="{ row }">
          <div class="progress">
            <div style="width: 176px;">
              <el-progress
                :percentage="calcPercent(row?.fieldData.length, row?.number)"
                :show-text="false"
                color="#1890FF"
                define-back-color="#F5F7FA"
              />
            </div>

            <span class="data">{{ row.fieldData.length }}/{{ row.number }}</span>
          </div>

        </template>
      </el-table-column>
      <el-table-column label="照片" align="center">
        <template slot-scope="scope">
          <el-image
            v-if="atLeastN(scope.row.photo, 1)"
            style="width: 40px; height: 30px"
            :src="scope.row.photo.length > 0 ? scope.row.photo[0] : []"
            :preview-src-list="scope.row.photo"
            fit="cover"
          />
          <el-tag v-else size="small" type="warning">未上传</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <div class="btn-context">
            <el-button
              size="small"
              type="text"
              @click="handleEditRoom(scope.row)"
            >
              编辑
            </el-button>

            <el-button
              size="small"
              type="text"
              style="color: #FF4949;"
              @click="handleRemovePerson(scope.row)"
            >
              删除
            </el-button>

            <el-button
              size="small"
              type="text"
              style="color: #13CE66;"
              @click="handleRoomInfo(scope.row)"
            >
              详情
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import { atLeastN, get } from '@/utils/kris_utils'
export default {
  props: {
    tableData: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
    }
  },
  methods: {
    calcPercent(part, total) {
      if (isNaN(total) || isNaN(part) || total <= 0 || part <= 0) return 0
      console.log('total, part --------->', total, part)
      console.log('(part / total) * 100 --------->', (part / total) * 100)
      return (part / total) * 100
    },
    atLeastN,
    handleRoomInfo(row) {
      this.$router.push({
        path: `/dormitory/room/details/${row.id}`
      })
    },
    async handleRemovePerson(row) {
      console.log(row, 'row')
      const message = row?.fieldData.length > 0 ? '这间房间已有人入住，您确定删除吗?' : '您确定要删除这间房间吗'
      try {
        await this.$confirm(message, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: row?.number > 0 ? 'error' : 'warning'
        })
        this.$emit('onDetRooms', row.id)
      } catch (error) {
        console.log(error, 'error')
      }
    },
    handleEditRoom(row) {
      this.$emit('onEditRooms', row)
    }
  }
}
</script>
<style lang="scss" scoped>
.showItem-dormitory-details {
  //padding: 20px 40px;
}
.btn-context{
  display: flex;
  justify-content: center;
  align-items: center;
}

.progress {
  display: flex;
  justify-content: center;
  align-items: center;
  .data {
    margin-left: 4px;
    color: #606266;
    font-size: 14px;
  }
}
</style>
